package cn.cvs.service.user;

import cn.cvs.dao.sysUser.SysUserMapper;
import cn.cvs.dao.user.UserMapper;
import cn.cvs.pojo.SysUser;
import cn.cvs.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
import java.util.List;

/**
 * service层捕获异常，进行事务处理
 * 事务处理：调用不同dao的多个方法，必须使用同一个connection（connection作为参数传递）
 * 事务完成之后，需要在service层进行connection的关闭，在dao层关闭（PreparedStatement和ResultSet对象）
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;

	@Override
	public int registerUser(User user) {
		return userMapper.insertUser(user);
	}

	@Override
	public int updateUser(User user) {
		return userMapper.updateUser(user);
	}

	@Override
	public User login(String username, String password) {
		User user = userMapper.selectByAccount(username);
		//匹配密码
		if(null != user){
			if(!user.getPassword().equals(password))
				user = null;
		}
		return user;
	}

	@Override
	public List<User> list(String name, Integer pageIndex, Integer pageSize) {
		int pageBean = (pageIndex - 1) * pageSize;
		return userMapper.list(name, pageBean, pageSize);
	}

	@Override
	public int count(String name) {
		return userMapper.count(name);
	}

	@Override
	public int deleteById(Integer id) {
		return userMapper.deleteById(id);
	}

	@Override
	public User getUserById(int id) {
		return userMapper.selectById(id);
	}


}
